   1. /****************************************************************************************/   
   2. /* Copyright � 2004 Altera Corporation, San Jose, California, USA. All rights reserved. */   
   3. /* Permission is hereby granted, free of charge, to any person obtaining a copy of this */   
   4. /* software and associated documentation files (the "Software"), to deal in the Software*/   
   5. /* without restriction, including without limitation the rights to use, copy, modify,   */   
   6. /* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to   */   
   7. /* permit persons to whom the Software is furnished to do so, subject to the following  */   
   8. /* conditions:  The above copyright notice and this permission notice shall be included */   
   9. /* in all copies or substantial portions of the Software.                               */   
  10. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,  */   
  11. /* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A        */   
  12. /* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT   */   
  13. /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */   
  14. /* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE */   
  15. /* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                        */   
  16. /* This agreement shall be governed in all respects by the laws of the State of         */   
  17. /* California and by the laws of the United States of America.  Altera does not         */   
  18. /* recommend, suggest or require that this reference design file be used in conjunction */   
  19. /* or combination with any other product.                                               */   
  20. /****************************************************************************************/   
  21.    
  22. //Includes   
  23. #include "altera_avalon_pwm_regs.h"   
  24. #include "altera_avalon_pwm_routines.h"   
  25. #include "system.h"   
  26. #include <stdio.h>   
  27. //Function Protypes   
  28. void print_error(unsigned int address, int return_code);   
  29. void check_return_code(unsigned int address, int return_code);   
  30.    
  31. int main(void)   
  32.    
  33. {   
  34.     
  35.  unsigned int duty_cycle;   
  36.  int return_code = ALTERA_AVALON_PWM_OK;   
  37.     
  38.  printf("Hello from the PWM test program.\n");   
  39.  printf("The starting values in the PWM registers are:\n");   
  40.  printf("Period = %u\n", IORD_ALTERA_AVALON_PWM_CLOCK_DIVIDER(Z_PWM_0_BASE) );   
  41.  printf("Duty cycle = %u\n", IORD_ALTERA_AVALON_PWM_DUTY_CYCLE(Z_PWM_0_BASE) );   
  42.    
  43.  printf("\nNotice the pulsing LED on the development board.\n");   
  44.     
  45.  //Initialize PWM and Check Return Code   
  46.  return_code = altera_avalon_pwm_init(Z_PWM_0_BASE, 500000, 1);     
  47.  check_return_code(Z_PWM_0_BASE, return_code);   
  48.     
  49.  //Enable PWM and Check Return Code   
  50.  return_code = altera_avalon_pwm_enable(Z_PWM_0_BASE);    
  51.  check_return_code(Z_PWM_0_BASE, return_code);   
  52.    
  53. //init duty_cycle with the value written to duty_cycle register during initialization   
  54.  duty_cycle = IORD_ALTERA_AVALON_PWM_DUTY_CYCLE(Z_PWM_0_BASE);   
  55.     
  56.     
  57.  while(1)   
  58.    {   
  59.      while(duty_cycle++  IORD_ALTERA_AVALON_PWM_CLOCK_DIVIDER(Z_PWM_0_BASE))   
  60.      {   
  61.         return_code = altera_avalon_pwm_change_duty_cycle(Z_PWM_0_BASE, duty_cycle);   
  62.         check_return_code(Z_PWM_0_BASE, return_code);   
  63.      }             
  64.      while(--duty_cycle > 1)   
  65.         altera_avalon_pwm_change_duty_cycle(Z_PWM_0_BASE, duty_cycle);   
  66.         check_return_code(Z_PWM_0_BASE, return_code);   
  67.    }   
  68. return 0;   
  69. }   
  70.    
  71. void check_return_code(unsigned int address, int return_code)   
  72. {   
  73.     if(return_code != ALTERA_AVALON_PWM_OK)   
  74.             print_error(address, return_code);     
  75. }   
  76.    
  77. void print_error(unsigned int address, int return_code)   
  78. {   
  79.     printf("Program Terminated Due to an error with Avalon PWM located at 0x%x:\n", address);   
  80.     switch(return_code)   
  81.     {   
  82.     case ALTERA_AVALON_PWM_DUTY_CYCLE_GREATER_THAN_CLOCK_CYCLE_ERROR:   
  83.         printf("The value in the clock cycle register must be greater than the value in the duty cycle register\n");   
  84.         printf("Value in the Clock Divide Register: 0x%x\n", IORD_ALTERA_AVALON_PWM_CLOCK_DIVIDER(address));    
  85.         printf("Value in the Duty Cycle Register: 0x%x\n", IORD_ALTERA_AVALON_PWM_DUTY_CYCLE(address));   
  86.         break;   
  87.         case ALTERA_AVALON_PWM_ENABLED_CONFIRMATION_ERROR:   
  88.                 printf("Unable to confirm that the PWM is enabled\n");   
  89.         printf("Value in the Enable Register: 0x%x\n", IORD_ALTERA_AVALON_PWM_ENABLE(address));    
  90.             break;   
  91.         case ALTERA_AVALON_PWM_DISABLED_CONFIRMATION_ERROR:   
  92.             printf("Unable to confirm that the PWM is disabled\n");   
  93.         printf("Value in the Enable Register: 0x%x\n", IORD_ALTERA_AVALON_PWM_ENABLE(address));    
  94.             break;   
  95.         default:   
  96.             break;              
  97.     }   
  98.    
  99. while(1);   
 100. }   
 101. // end of file   

